package base.byteorchar.arrays;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Byte & Char Arrays
 */
public class Client {

    static void reading() throws IOException {
        byte[] bytes = new byte[1024];
        // 1. write data into byte array...
        InputStream inputStream = new ByteArrayInputStream(bytes);

        // 2. InputStream inputStream1 = new CharArrayReader(new char[]);

        // read first byte
        int data = inputStream.read();

        while (data != -1) {
            // do something with data

            // read next byte
            data = inputStream.read();
        }
    }

    static void writing() throws IOException {
        // 1. arrays
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        output.write("This text is converted to bytes".getBytes(Charset.forName("UTF-8")));
        byte[] bytes = output.toByteArray();

        // 2. chars
        CharArrayWriter writer = new CharArrayWriter();
        writer.write("This text is converted to chars".toCharArray());
        char[] chars = writer.toCharArray();
    }
}

